<template>
  <div>
    <el-drawer title="签到表" :visible.sync="drawer" :direction="direction">
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        style="padding-right: 20px"
      >
        <el-form-item label="签到主题" prop="title">
          <el-input v-model.trim="form.title"></el-input>
        </el-form-item>
        <el-form-item label="应到人数">
          <el-input-number
            v-model="form.dueNum"
            :min="1"
            :max="1000"
            size="medium"
            label="应到人数"
          ></el-input-number>
        </el-form-item>
        <!-- <el-form-item label="开始时间">
          <el-date-picker
            v-model="form.startTime"
            type="date"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="截止时间">
          <el-date-picker
            v-model="form.endTime"
            type="date"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="签到时间">
          <el-date-picker
            v-model="form.signTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model.trim="form.addr"></el-input>
        </el-form-item>
      </el-form>
      <el-button
        type="custom"
        v-preventReClick
        @click="submitForm('form')"
        style="margin-left: 100px"
        >保 存</el-button
      >
    </el-drawer>
  </div>
</template>
<script>
export default {
  data() {
    return {
      drawer: false,
      direction: 'rtl',
      form: {
        title: '', //签到主题
        resId: '', //资源id
        resType: '1', //资源类型1-章节，2-其他扩展
        actNum: '100', //实到人数
        dueNum: '1', //应到人数
        startTime: '', //开始签到时间 2023-04-13 9:00
        endTime: '', //截止截止时间 2023-04-13 9:30
        addr: '', //地址
        signTime: '',
      },
      rules: {
        title: [{ required: true, message: '请输入签到主题', trigger: 'blur' }],
      },
      current: {},
      isAdd: false,
    }
  },
  props: ['curLessonPlan'],
  mounted() {
    this.$bus.$on('showSignIn', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('showSignIn')
  },
  methods: {
    async init(row) {
      let obj = {
        page: 1,
        limit: 99,
        resId: row.id,
        resType: '1',
      }
      let res = await this.$store.dispatch('queryeduSignIn', obj)
      let data = res.data
      this.isAdd = data.length == 0
      if (this.isAdd) {
        this.form = this.$options.data().form
        this.form.title = row.name
        this.$delete(this.form, 'id')
      } else {
        this.current = data[0]
        let { id, startTime, endTime } = this.current
        this.form = { ...this.form, ...this.current }
        this.$set(this.form, 'id', id)
        if (startTime && endTime) {
          this.form.signTime = [startTime, endTime]
        }
      }
      this.drawer = true
    },
    submitForm(form) {
      this.form.startTime = this.form.signTime[0]
      this.form.endTime = this.form.signTime[1]
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let dispatchType = this.isAdd ? 'addeduSignIn' : 'updateeduSignIn'
          await this.$store.dispatch(dispatchType, this.form)
          this.successMsg('操作成功')
          // this.$bus.$emit('refreshLessonPlanList')
          this.drawer = false
        }
      })
    },
  },
}
</script>
<style scoped lang="scss">
</style>